<?php
namespace soa\parter\sto;
use artisan\http;

/**
 * 申通订单类
 * Class EntryOrder
 * @package soa\parter\zt
 */
class Order
{
    /**
     * 服务地址
     */
//    protected $service_url = 'http://szdqh.wicp.net:9096/stoPlatform/app/orderState!saveOrderState.action';   //测试
    protected $service_url = 'http://222.66.39.203:22221/stoPlatform/app/orderState!saveOrderState.action';   //正式
//    protected $service_url = 'http://222.66.109.134:22226/stoPlatform/app/orderState!saveOrderState.action';   //测试

    /**
     * 公司名称
     */
    protected $company_id = 'KUAIBAO';

    /**
     * 传输校验的key值
     */
    protected $key = '0bdbb2c325525e986d55ef50ca66d056';

    /**
     * 接口类型
     */
    protected $msg_type = '';


    /**
     * 初始化
     * EntryOrder constructor.
     */
    public final function __construct(array $param)
    {
        if (empty($param) || !is_array($param)) {
            return null;
        }
        $this->msg_type = $param['msg_type'];
        return $this;
    }


    /**
     * 获取服务名称
     */
    public function getCompanyId()
    {
        return $this->company_id;
    }

    /**
     * 获取服务地址
     */
    public function getServiceUrl()
    {
        return $this->service_url;
    }

    /**
     * 获取校验key
     */
    public function getKey()
    {
        return $this->key;
    }

    /**
     * 接口类型
     */
    public function msg_type()
    {
        return $this->msg_type;
    }


    /**
     * 发送请求
     * @param $data
     * @return mixed
     */
    public function requestApi($data)
    {
        $data = mb_convert_encoding(json_encode($data), 'UTF-8');             //转UTF-8
        $data_digest = (base64_encode(md5($data . $this->getKey())));  //签名       签名方式 base64(md5(data字段+key))
        $param = array(
            'data_digest'         => $data_digest,            //签名
            'eccompanyid'         => $this->getCompanyId(),   //电商id
            'msg_type'            => $this->msg_type(),       //接口类型
            'logistics_interface' => $data,                   //数据
        );
        return http::post($this->getServiceUrl(),$param);
    }

    /**
     * 拒单/回填单号
     * @param $params
     * @return mixed
     */
    public function refuseOfRewriteOrderOfSto($params)
    {
        $time = date('Y-m-d H:i:s');
        $orders = array();
        $errorOrders = array();
        foreach ($params as $param){
            $isRefuseOrder = $this->isRefuseOrder($param['status']);    //检查该订单是否可拒单
            if(!$isRefuseOrder){    //若订单不可操作，拼装错误信息，用于返回，并continue该次循环
                $errorOrders[] = array(
                    'thirdOrderNo'=>$param['third_party_orderid'],
                    'success'=>'false',
                    'reason'=>'该订单不可操作'
                );
                continue;
            }
            logDebug('third_party_orderid:'.$param['third_party_orderid'].'--orderstatus:'.$param['status'].'--isRefuseOrder:'.$isRefuseOrder,'refuseOfRewriteOrderOfSto of 1031');
            $orders[] = array(
                'serialId'      => $param['third_party_of_id'],      //第三方流水号
                'orderNo'       => $param['third_party_orderid'],    //第三方订单号
                'operateBranch' => $param['shopName'],     //操作网点名
                'operator'      => $param['courierName'],  //操作人 快递员姓名
                'operateTime'   => $time,
                'operateType'   => $param['operateType'],         //操作类型 1、打回  2、绑定运单
                'waybillNo'     => (isset($param['waybillNo']) && !empty($param['waybillNo']))?$param['waybillNo']:'',           //运单编号 打回时可不填
                'backReason'    => in_array($param['refuseMsg'],array(1,2,3))?$param['refuseMsg']:'',  //打回原因 1、地址非本人揽件范围 2、客户更改取件时间 3、客户取消订单
            );
        }
        $data = array(
            'eccompanyid' => $this->getCompanyId(),   //公司名
            'orders' => $orders,
        );
        $response = json_decode($this->requestApi($data),true);   //发送请求
        logDebug('operateType:'.$param['operateType'].'--'.json_encode($response),'refuseOfRewriteOrderOfSto of response');   //采集所有response
        if(empty($response)){
            return array();
        }
        if(!empty($errorOrders)){
            $response = array_merge($response['responseItems'],$errorOrders);     //将无法操作订单与正常打回的订单信息合并
        }
        return $response;
    }


    /**
     * 检查该订单是否可拒单
     * @param $status
     * @return bool
     */
    public function isRefuseOrder($status)
    {
        $allStatus = ['-8','8','9'];    //订单不可操作状态
        if(in_array($status,$allStatus)){
            return false;
        }
        return true;
    }


}